package com.lht.leetcodeDemo;

import org.junit.Test;

/**
 * @author Eric
 * @version 1.0
 * @date 2019-05-27
 */
public class RemoveDuplicatesfromSortedList {

    @Test
    public void test(){
        int[] arr=new int[]{1,1,1};

        ListNode head=new ListNode(0);
        ListNode cur=head;
        for(int i=0;i<arr.length;i++){
            cur.next=new ListNode(arr[i]);
            cur=cur.next;
        }

        head=deleteDuplicatesTwo(head);
        while (head != null) {
            System.out.print(head.val+",");
            head = head.next;
        }
    }

    public ListNode deleteDuplicates(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        if(head.val==head.next.val){
            head.next=head.next.next;
            //等的时候一直调用删除
            deleteDuplicates(head);
        }else{
            //不等则跳到下一个
            deleteDuplicates(head.next);
        }
        return head;
    }

    public ListNode deleteDuplicatesTwo(ListNode head) {
        ListNode cur=head;
        while(cur!=null&&cur.next!=null){
            if(cur.val==cur.next.val){
                cur.next=cur.next.next;
            }else{
                cur=cur.next;
            }
        }
        return head;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
